package com.rerun.service.impl;

import com.rerun.common.easemob.api.impl.EasemobIMUsers;
import com.rerun.entity.ChatUser;
import com.rerun.entity.MemberUser;
import com.rerun.entity.YoyoPark;
import com.rerun.mapper.ChatUserMapper;
import com.rerun.service.IChatService;
import com.rerun.service.IMemberUserService;
import com.rerun.service.IYoyoParkService;
import io.swagger.client.model.RegisterUsers;
import io.swagger.client.model.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.Random;

/**
 * @author dax.
 * @version v1.0
 * @since 2017/10/30 16:04
 */
@Service
public class ChatServiceImpl implements IChatService {
    private EasemobIMUsers easemobIMUsers = new EasemobIMUsers();
    @Resource
    private IMemberUserService iMemberUserService;
    @Resource
    private IYoyoParkService iYoyoParkService;
    @Resource
    private ChatUserMapper chatUserMapper;

    @Override
    public Object getMemberChatUser(String memberId) {

        ChatUser chatUser = chatUserMapper.findByMemberId(memberId);
        if (Objects.nonNull(chatUser)) {
            return chatUser;
        }
        MemberUser memberUser = iMemberUserService.selectById(memberId);
        if (Objects.nonNull(memberUser)) {
            Random random = new Random();
            String userNameTemp = memberUser.getId();
            int r=random.nextInt(500);
            String userName=userNameTemp.length()>12? userNameTemp.substring(0,11)+r:userNameTemp+r;
            String pwd = new Random().nextInt(500) + userName;

            User user = new User();
            user.setUsername(userName);
            user.setPassword(pwd);

            RegisterUsers users = new RegisterUsers();
            users.add(user);
            Object object = easemobIMUsers.createNewIMUserSingle(users);

            if (Objects.nonNull(object)) {
                ChatUser chatUserInfo = new ChatUser();
                        chatUserInfo.setMemberId(memberId);
                        chatUserInfo.setCreateTime(new Date());
                        chatUserInfo.setUserName(userName);
                        chatUserInfo.setuPassword(pwd);
                chatUserMapper.insert(chatUserInfo);
                return chatUserInfo;
            }
        }
        return null;
    }

    @Override
    public Object getParkChatUser(long parkId) {
        ChatUser chatUser = chatUserMapper.findByParkId(parkId);
        if (Objects.nonNull(chatUser)) {
            return chatUser;
        }
        YoyoPark yoyoPark = iYoyoParkService.selectById(parkId);
        if (Objects.nonNull(yoyoPark)) {
            Random random = new Random();
            String userName  = String.valueOf(parkId) ;

            String pwd = random.nextInt(500000) + userName;

            User user = new User();
            user.setUsername(userName);
            user.setPassword(pwd);

            RegisterUsers users = new RegisterUsers();
            users.add(user);
            Object object = easemobIMUsers.createNewIMUserSingle(users);
            if (Objects.nonNull(object)) {
                ChatUser chatUserInfo = new ChatUser();
                chatUserInfo.setParkId(parkId);
                chatUserInfo.setCreateTime(new Date());
                chatUserInfo.setUserName(userName);
                chatUserInfo.setuPassword(pwd);
                chatUserMapper.insert(chatUserInfo);
                return chatUserInfo;
            }
        }
        return null;
    }
}
